package com.woniu.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageResult;
import com.woniu.product.entity.ChargeStationQuery;
import com.woniu.product.mapper.ChargePileMapper;
import com.woniu.product.mapper.ChargeStationMapper;
import com.woniu.product.pojo.ChargePile;
import com.woniu.product.pojo.ChargeStation;
import com.woniu.product.service.IChargePileService;
import com.woniu.product.service.IChargeStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 充电站表 服务实现类
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-20
 */
@Service
public class ChargeStationServiceImpl extends ServiceImpl<ChargeStationMapper, ChargeStation> implements IChargeStationService {
    @Autowired(required = false)
    private ChargeStationMapper chargeStationMapper;

    @Override
    public void addChargeStation(ChargeStation chargeStation) {
        chargeStationMapper.insert(chargeStation);
    }

    @Override
    public void deleteChargeStation(String id) {
        chargeStationMapper.deleteById(id);
    }

    @Override
    public void updateChargePile(ChargeStation chargeStation) {
        chargeStationMapper.updateById(chargeStation);
    }

    @Override
    public PageResult<ChargeStation> queryChargeStation(Integer currentPage, Integer pageSize, ChargeStationQuery chargeStationQuery) {
        IPage<ChargeStation> page = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<ChargeStation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(chargeStationQuery.getId()) , ChargeStation::getId , chargeStationQuery.getId())
                .eq(!StringUtils.isEmpty(chargeStationQuery.getProvince()) , ChargeStation::getProvince , chargeStationQuery.getProvince())
                .eq(!StringUtils.isEmpty(chargeStationQuery.getCity()) , ChargeStation::getCity , chargeStationQuery.getCity())
                .eq(!StringUtils.isEmpty(chargeStationQuery.getArea()) , ChargeStation::getArea , chargeStationQuery.getArea())
                .eq(!StringUtils.isEmpty(chargeStationQuery.getStreet()) , ChargeStation::getStreet , chargeStationQuery.getStreet())
                .eq(!StringUtils.isEmpty(chargeStationQuery.getStatus()) , ChargeStation::getStatus , chargeStationQuery.getStatus())
                .gt(!StringUtils.isEmpty(chargeStationQuery.getStartTime()) , ChargeStation::getCreatedTime , chargeStationQuery.getStartTime())
                .lt(!StringUtils.isEmpty(chargeStationQuery.getEndTime()) , ChargeStation::getCreatedTime , chargeStationQuery.getEndTime());
        IPage<ChargeStation> selectPage = chargeStationMapper.selectPage(page, wrapper);
        return new PageResult<ChargeStation>(selectPage.getTotal(),selectPage.getRecords());
    }
}
